<template>
  <div>
    <el-form :model="tableData" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-card class="box-card">
        <span>基本信息</span>
        <div class="text item">

          <el-form-item label="联系人" prop="name">
            <el-input v-model="tableData.name" :maxlength='10' placeholder="不超过10个字符..." :disabled="contactsManageData.isAdd"></el-input>
          </el-form-item>


          <el-form-item label="联系人手机号" prop="phone">
            <el-input v-model="tableData.phone" :maxlength='11' placeholder="不超过11个字符..." :disabled="contactsManageData.isAdd"></el-input>
          </el-form-item>
          <el-form-item label="备注" >
            <el-input v-model="tableData.remark" type='textarea' placeholder="不超过100个字符..." :maxlength='100' :disabled="contactsManageData.isAdd"></el-input>
          </el-form-item>
            


        </div>
      </el-card>

      <el-form-item class='align'>
        <el-button type="primary" @click="resetForm('ruleForm')">返回</el-button>
        <el-button type="success" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import agentHttp from '@/utils/agentHttp.js'
  import {require,phone} from '@/utils/rules.js';
  export default {
    data() {
      return {
        tableData: {
          name:"",
          phone:"",
          remark:"",
          fkId:this.contactsManageData.id,
        },
        rules: {
          name: [require("联系人")],
          phone: [require('手机号码', {trigger: "blur"}), {validator: phone,trigger: "blur"}],
        }
      }
    },
    methods: {
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.contactsManageData.isCode = false;
      },
      submitForm(formName) { //提交数据
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.contactsManageData.isMsg == "edit") {
              //修改

              agentHttp.putJson(`adminApi/admin/up_supplier/phone_update`, this.tableData).then(data => {
                if (data.success) {
                  this.contactsManageData.isCode = false;
                  this.$message.success(data.data || "修改成功");
                  this.$emit('Refresh', null);
                  this.contactsData.isCode = true;
                  
                }
              })
              return
            }

            agentHttp.postJson(`adminApi/admin/up_supplier/phone_add`, this.tableData).then(data => {
              if (data.success) {
                this.contactsManageData.isCode = false;
                this.$message.success(data.data || "添加成功");
                this.$emit('Refresh', null);
                  this.contactsData.isCode = true;

              }
            })
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      initData(){
        agentHttp.get(`adminApi/admin/up_supplier/phone_view/${this.contactsManageData.id}`).then((data) => {
          if (data.success) {
            this.tableData = data.data.view ;
           
          }
        })
      }
    },
    props: {
      contactsManageData:{
        type:Object
      },
      contactsData:{
        type:Object
      },
    },
    mounted(){
      console.dir(this.contactsManageData)
      switch(this.contactsManageData.isMsg){
            case "edit" :
            this.initData();
            break;
            case "add" :
            break;
            default:
            break;
      }
    }
  }
</script>

<style lang="scss" scoped>
 .align {
    margin: 50px auto 0;
    text-align: center;
    margin-left:-125px;
  }
</style>


